package com.wsd.smartcarwasher.domain;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.ArrayList;
import java.util.List;

/**
 * 后台对数据进行分页
 *
 * @param <T> 数据类型
 */
@ApiModel(value = "分页")
public class Pagination<T> {

    private static final int DEFAULT_PAGE_SIZE = 20;

    @ApiModelProperty(value = "总数")
    public int totalSize;

    @ApiModelProperty(value = "每页显示条数")
    public int pageSize;

    @ApiModelProperty(value = "页码")
    public int pageNum;

    @ApiModelProperty(value = "显示项目数")
    public List<T> resultList;

    public Pagination(int pageNum, int pageSize, List<T> totalData) {
        if (pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize <= 0) {
            pageSize = DEFAULT_PAGE_SIZE;
        }
        if (totalData == null) {
            totalData = new ArrayList<>();
        }
        this.totalSize = totalData.size();
        this.pageSize = pageSize;
        this.pageNum = pageNum;
        this.resultList = filterDataInPage(pageNum, pageSize, totalData);
    }

    private List<T> filterDataInPage(int pageNum, int pageSize, List<T> totalData) {
        List<T> resultList = new ArrayList<>();
        for (int index = (pageNum - 1) * pageSize; index < totalData.size() && index < pageNum * pageSize; index++) {
            resultList.add(totalData.get(index));
        }
        return resultList;
    }



    public Pagination(int pageNum, int pageSize,int totalSize, List<T> totalData) {
        if (pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize <= 0) {
            pageSize = DEFAULT_PAGE_SIZE;
        }
        if (totalData == null) {
            totalData = new ArrayList<>();
        }
        this.totalSize = totalSize;
        this.pageSize = pageSize;
        this.pageNum = pageNum;
        this.resultList = totalData;
    }
}
